package com.appkubes.fintech.restful.application.pay.wechat.exception;

import com.google.common.base.Joiner;

/**
 * @author ocean
 * @time 2019-07-04 17:46
 * 功能：
 **/
public class WeChatException extends Exception {
    private static final long serialVersionUID = 2214381471513460742L;
    private String customErrorMsg;
    private String returnCode;
    private String returnMsg;
    private String resultCode;
    private String errCode;
    private String errCodeDes;
    private String xmlString;

    public WeChatException(String customErrorMsg) {
        super(customErrorMsg);
        this.customErrorMsg = customErrorMsg;
    }

    public WeChatException(String customErrorMsg, Throwable tr) {
        super(customErrorMsg, tr);
        this.customErrorMsg = customErrorMsg;
    }

    private WeChatException(WeChatException.Builder builder) {
        super(builder.buildErrorMsg());
        this.returnCode = builder.returnCode;
        this.returnMsg = builder.returnMsg;
        this.resultCode = builder.resultCode;
        this.errCode = builder.errCode;
        this.errCodeDes = builder.errCodeDes;
        this.xmlString = builder.xmlString;
    }

    public static final class Builder {
        private String returnCode;
        private String returnMsg;
        private String resultCode;
        private String errCode;
        private String errCodeDes;
        private String xmlString;

        private Builder() {
        }

        public WeChatException.Builder returnCode(String returnCode) {
            this.returnCode = returnCode;
            return this;
        }

        public WeChatException.Builder returnMsg(String returnMsg) {
            this.returnMsg = returnMsg;
            return this;
        }

        public WeChatException.Builder resultCode(String resultCode) {
            this.resultCode = resultCode;
            return this;
        }

        public WeChatException.Builder errCode(String errCode) {
            this.errCode = errCode;
            return this;
        }

        public WeChatException.Builder errCodeDes(String errCodeDes) {
            this.errCodeDes = errCodeDes;
            return this;
        }

        public WeChatException.Builder xmlString(String xmlString) {
            this.xmlString = xmlString;
            return this;
        }

        public WeChatException build() {
            return new WeChatException (this);
        }

        public String buildErrorMsg() {
            return Joiner.on("，").skipNulls().join(this.returnCode == null ? null : String.format("返回代码：[%s]", this.returnCode), this.returnMsg == null ? null : String.format("返回信息：[%s]", this.returnMsg), new Object[]{this.resultCode == null ? null : String.format("结果代码：[%s]", this.resultCode), this.errCode == null ? null : String.format("错误代码：[%s]", this.errCode), this.errCodeDes == null ? null : String.format("错误详情：[%s]", this.errCodeDes), this.xmlString == null ? null : "微信返回的原始报文：\n" + this.xmlString});
        }
    }
}
